#-*- coding:utf-8 -*-

import hashlib
import zlib

sn = '000001'
tmp_prefix = 'HFS0'
vender_id = '4CE1BB'
def gen_uid(sn):
    sn = '%06d' % int(sn)
    tmp_id = tmp_prefix + vender_id + sn
    m = hashlib.md5()
    m.update(tmp_id)
    chk_sum = hex(zlib.crc32(m.hexdigest().upper()))
    chk_sum = (chk_sum[2:4] + chk_sum[-2:]).upper()
    return tmp_id.replace(tmp_prefix, chk_sum, 1)
    
i = 10
while i > 0:
    print gen_uid(i)
    i -= 1

'''
OUTPUT:
----------------
18B34C1EBB000010
42454C1EBB000009
4EDC4C1EBB000008
X4D24C1EBB000007
19DA4C1EBB000006
X59B4C1EBB000005
X2E04C1EBB000004
X2DC4C1EBB000003
X7A64C1EBB000002
7CDE4C1EBB000001
'''

'''
v1, v2 区别：
1 去掉厂商标识
2 序列号改为8位16进制
3 总长度缩短为12位
'''

tmp_prefix = '07FA' # 任意
def gen_uid_v2(sn):
    sn = ('%08x' % int(sn)).upper()
    tmp_id = tmp_prefix + sn
    m = hashlib.md5()
    m.update(tmp_id)
    chk_sum = hex(zlib.crc32(m.hexdigest().upper()))
    chk_sum = (chk_sum[2:4] + chk_sum[-2:]).upper()
    return tmp_id.replace(tmp_prefix, chk_sum, 1)


i = 10
while i > 0:
    print gen_uid_v2(i)
    i -= 1

'''
OUTPUT:
------------
63DA0000000A
X30B00000009
1E2400000008
6FF600000007
X49B00000006
738200000005
63CE00000004
X7A900000003
X48300000002
9CBB00000001
'''

def checkout_uid_v2(uid):
    chk_sum = uid[:4]
    sn = int(uid[4:], 16)
    return uid == gen_uid_v2(sn)

print checkout_uid_v2('63DA0000000A')
print checkout_uid_v2('63DA0000000B')
